<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="getList">
        查询
      </el-button>
      <el-button class="filter-item" type="success" icon="el-icon-edit" @click="handleAdd">
        添加
      </el-button>
    </div>
      <el-table :data="list" border fit highlight-current-row row-key="id" style="width: 100%;">
        <el-table-column label="公司名称" prop="clientCompanyName" align="center" />
        <el-table-column label="手机号" prop="clientMobile" align="center" />
        <el-table-column label="联系人" prop="clientName" align="center" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button type="primary" size="mini" @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button type="danger" size="mini" @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    
    <el-dialog :title="title" :visible.sync="dialogFormVisible" append-to-body>
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="88px"
        style="margin: 0 50px;">
        <el-form-item label="公司名称" prop="clientCompanyName">
          <el-input v-model="temp.clientCompanyName" />
        </el-form-item>
        <el-form-item label="手机号" prop="clientMobile">
          <el-input v-model="temp.clientMobile" />
        </el-form-item>
        <el-form-item label="客户名称" prop="clientName">
          <el-input v-model="temp.clientName" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="saveData()">
          确定
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getAgentDetailList, saveAgentDetail, updateAgentDetail, deleteAgentDetail } from '@/api/product'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'agentList',
  directives: { waves },
  components: { Pagination },
  filters: {
  },
  data() {
    return {
      list: [],
      listLoading: true,
      temp: {
        clientCompanyName: '',
        clientMobile: '',
        clientName: '',
        clientType: 1
      },
      rules: {
        clientCompanyName: [{ required: true, message: '请输入', trigger: 'blur' }],
        clientMobile: [{ required: true, message: '请输入', trigger: 'blur' }],
        clientName: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
      dialogFormVisible: false,
      title: ''
    }
  },
  watch: {
  },
  computed: {
    ...mapGetters(["userInfo"]),
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      let params = {gongyingId: this.userInfo.id, type: 1, clientType: 1}
      getAgentDetailList(params).then(response => {
        this.list = response
        this.listLoading = false
      })
    },
    handleAdd() {
      this.temp.clientCompanyName = ''
      this.temp.clientMobile = ''
      this.temp.clientName = ''
      this.dialogFormVisible = true
      this.title = "添加"
    },
    handleEdit(row) {
      this.temp = Object.assign({}, row)
      this.dialogFormVisible = true
      this.title = "编辑"
    },
    handleDelete(row) {
      deleteAgentDetail({ids: row.id}).then(response => { 
        this.getList()
        this.$message({
          message: '操作成功',
          type: 'success'
        });
      }).catch(error => {
        console.log(error);
      })
    },
    saveData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.title === '添加') {
            delete this.temp.createTime
            this.temp.clientType = 1
            saveAgentDetail(this.temp).then(() => {
              this.getList()
              this.dialogFormVisible = false

              this.$message({
                message: '保存成功',
                type: 'success'
              });
            })
          } else {
            delete this.temp.createTime
            this.temp.clientType = 1
            updateAgentDetail(this.temp).then(() => {
              this.getList()
              this.dialogFormVisible = false

              this.$message({
                message: '保存成功',
                type: 'success'
              });
            })
          }
        }
      })
    },
  }
}
</script>
